package com.etrans.web.gps.business.platformTask.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.etrans.web.gps.business.platformTask.domain.PlatformAnalyzes;
import com.etrans.web.gps.business.platformTask.domain.PlatformVO;
import com.etrans.web.gps.business.platformTask.service.SendMessageService;
import com.etrans.web.gps.util.DateUtil;

@Controller
@RequestMapping("/mail/platform/*")
public class PlatformController {
	
	@Autowired
	private SendMessageService sendMessageService;
	
	/**
	 * 加载考核信息页面
	 */
	@RequestMapping(value="platformQC.html",produces="text/html;charset=UTF-8")
	public void platformAnalyzesQC(HttpServletRequest request,HttpServletResponse response, String timeType, 
			String year,String time,String platformId, String queryTime){
		//设置响应信息格式
		response.setContentType("text/html;charset=UTF-8");  
	    response.setCharacterEncoding("utf-8");
		//查询数据
		List<PlatformAnalyzes> platSwapList = new ArrayList<PlatformAnalyzes>();
		// 根据id查询平台基本信息
		PlatformVO platform = sendMessageService.queryPlatformById(platformId);
		String start_end = "";
		if ("week".equals(timeType)) {
			Map<String,Object> lastWeek = DateUtil.getYearAndWeekByDate(Integer.valueOf(year),Integer.valueOf(time));
			Date lastMonday = DateUtil.getStringToDate(String.valueOf(lastWeek.get("Monday")));
			Date lastSunday = DateUtil.getStringToDate(String.valueOf(lastWeek.get("Sunday")));
			start_end = "(" + DateUtil.getDateToString(lastMonday, "MM月dd日")
							 + "-"+ DateUtil.getDateToString(lastSunday, "MM月dd日") + ")";
			platSwapList = sendMessageService.queryPlatAnalyzesWeek(queryTime);
		}
		if ("month".equals(timeType)) {
			platSwapList = sendMessageService.queryPlatAnalyzesMonth(queryTime);
		}
		
		try {
			//数据拼接
			HashMap<String,Object> model = new HashMap<String,Object>();
			DecimalFormat dft = new DecimalFormat("0.00");
			for (PlatformAnalyzes platformAna : platSwapList) {
				String gnss_centerid = platformAna.getGnss_centerid();
				if (gnss_centerid.equals(String.valueOf(platform.getGnss_centerid()))) {
			        double dealRate = platformAna.getAlarm_count() > 0 && platformAna.getAlarm_processed() > 0 ?
			        		platformAna.getAlarm_processed()/platformAna.getAlarm_count() : 0 ;
	        		 String dealRateStr = "0";
	     	        if (dealRate >= 1.00) {
	     	        	dealRateStr = "100";
	     			}else if (dealRate > 0) {
	     				dealRateStr = dft.format(dealRate);
	     			}
			        model.put("dealRate",dealRateStr+"%");
			        model.put("platform", platformAna);
			        model.put("contacts", platform.getContacts());
				}
			}
			model.put("year", year);
			model.put("time", time);
			model.put("start_end", start_end);
	        StringBuffer platStr = new StringBuffer();
	        for (PlatformAnalyzes platAna : platSwapList) {
	        	platStr.append("<tr>");
	        	//"排名:"
	        	platStr.append("<td>"+platAna.getRanking()+"</td>");
	        	//"得分:"
	        	platStr.append("<td>"+platAna.getScore()+"</td>");
	        	//平台名称
	        	platStr.append("<td>"+platAna.getPlatform_name()+"</td>");
	        	//"平台连通率:"
	        	platStr.append("<td>"+platAna.getOnline_rate()+"</td>");
				//"车辆总数:"
	        	platStr.append("<td>"+platAna.getVehicle_count()+"</td>");
				//"入网车辆数:"
	        	platStr.append("<td>"+platAna.getVehicle_access_count()+"</td>");
				//"车辆入网率:"
				int vehicle_count = platAna.getVehicle_count();
				platStr.append("<td>"+(vehicle_count <= 0 ? 0.00 : dft.format(platAna.getVehicle_access_rate()))+"</td>");
				//"车辆上线率:"
				platStr.append("<td>"+(vehicle_count <= 0 ? 0.00 : dft.format(platAna.getVehicle_online_rate()))+"</td>");
				//"数据合格率:"
				int trackNum = platAna.getTrack_normal()+platAna.getException_track_count();
				platStr.append("<td>"+(trackNum <= 0 ? 0.00 :dft.format(100.00*platAna.getTrack_normal()/trackNum))+"</td>");
				//"轨迹完整率:"
				platStr.append("<td>"+dft.format(platAna.getTrack_complete_rate())+"</td>");
				//"定位漂移车辆比率:"
				int sum = platAna.getLocation_exception_count()+platAna.getSpeed_160_count()+platAna.getGpstime_exception_count()+platAna.getHead_exception_count();
				double avg = sum<=0 ? 0 : (100.00 *platAna.getLocation_exception_count()) / sum;
				double result = platAna.getVehicle_online_count() <= 0 ? 0 : (avg*platAna.getException_vehicle_count())/platAna.getVehicle_online_count();
				platStr.append("<td>"+dft.format(result)+"</td>");
				platStr.append("</tr>");
	        }
	        model.put("resultList",platStr.toString());
	        PrintWriter out = response.getWriter();
			Object resultJSON = JSONObject.toJSON(model); //根据需要拼装json  
			String jsonpCallback = request.getParameter("jsonpCallback");//客户端请求参数  
			out.println(jsonpCallback+"("+resultJSON+")");//返回jsonp格式数据  
			out.flush();  
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}       
	}
	
	/**
	 * 跳转考核信息页面
	 */
	@RequestMapping("plat.html")
	public String toPlatformAnalyzesQC() {
		return "views/platformQC";
	}
}
